var util = require('../../libs/util.js');
var app = getApp();
Page({
  data: {
    warehouseData: [],
    searchShowed: false,
    searchVal: "",
    selected: false,
    index: '',
    pageSize: 10,
    pageNumber: 0,
    count: 0,
    page: 1,
    firstPage: true,
    prevPage: true,
    nextPage: true,
    lastPage: true,
  },
  onShareAppMessage: function () {
    return {
      title: app.appName,
      path: '/pages/index/index'
    }
  },
  onLoad: function (e) {
    wx.setNavigationBarTitle({ "title": app.appName +"--选择挂车" });
    wx.removeStorage({
      key: 'path_warehouse',
      success: function (res) {
      }
    });
    this.setData({
      CO_ID:e.CO_ID,
      vehicleId:e.id
    });
    this.loadData();
  },
  onReady: function () {
    wx.hideToast()
  },
  bindInput: function (e) {
    var item = String(e.currentTarget.id), item_value = {};
    item_value[item] = e.detail.value;
    this.setData(item_value);
  },
  search: function () {
    this.loadData();
  },
  loadData() {
    wx.showLoading({
      title: '加载数据中...',
    });
    var that = this;
    util.getData("/atapi/vehicle/vehicle/semitrailer", {
      CO_ID: app.globalData.CO_ID,
      'searchVal': that.data.searchVal,
      'page': that.data.page,
      'pageSize': that.data.pageSize
    }, function (res, error) {
      console.log(res); console.log(error)
      if (res.data.success) {
        var count = res.data.data[1][0].COUNT;
        var pageNumber = Math.ceil(count / that.data.pageSize);
        that.setData({
          dataList: res.data.data[0],
          count: count,
          pageNumber: pageNumber,
          page: that.data.page == 0 ? 1 : that.data.page
        });
        that.setPageStatus();
      }
      wx.hideLoading();
    });
  },
  dataSelect: function (e) {
    var that = this;
    var dataList = this.data.dataList;
    for (var i = 0; i < dataList.length; i++) {
      dataList[i].checked = dataList[i].ID == e.detail.value;
      if (dataList[i].ID == e.detail.value) {
        dataList[i].vehicleId = that.data.vehicleId;
        wx.setStorage({
          key: "semitrailer_select",
          data: dataList[i]
        });
        this.setData({
          selected: true
        })
      }
    }
    this.setData({
      dataList: dataList
    });
  },
  confirmData: function () {
    if (this.data.selected) {
      wx.navigateBack({
        delta: 1
      });
    } else {
      wx.showToast({
        title: "请选择挂车",
        image: "/images/wrong.svg",
        duration: 3000
      });
    }
  },
  //翻页按钮状态
  setPageStatus: function () {
    var that = this;
    var page = that.data.page, pageNumber = that.data.pageNumber;
    if (pageNumber == 1) {
      that.setData({
        firstPage: true,
        prevPage: true,
        nextPage: true,
        lastPage: true
      });
    } else if (pageNumber > 1) {
      if (page == 1) {
        that.setData({
          firstPage: true,
          prevPage: true,
          nextPage: false,
          lastPage: false
        });
      } else if (page == pageNumber) {
        that.setData({
          firstPage: false,
          prevPage: false,
          nextPage: true,
          lastPage: true
        });
      } else if (page < pageNumber && page > 1) {
        that.setData({
          firstPage: false,
          prevPage: false,
          nextPage: false,
          lastPage: false
        });
      }
    }
  },
  //分页按钮操作
  pageChange: function (e) {
    var type = e.currentTarget.dataset.page;
    if (type == 'first') {
      this.setData({
        page: 1
      })
      this.loadData();
    }
    if (type == 'last') {
      this.setData({
        page: this.data.pageNumber
      })
      this.loadData();
    }
    if (type == 'prev') {
      this.setData({
        page: this.data.page - 1
      })
      this.loadData();
    }
    if (type == 'next') {
      this.setData({
        page: this.data.page + 1
      })
      this.loadData();
    }
  },
  cancelConfirmData: function () {
    wx.removeStorage({
      key: 'semitrailer_select',
      success (res) {
        console.log(res)
      }
    })
    wx.navigateBack({
      delta: 1
    });
  }
});